package com.bajiao.admin.controller.discountRule;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.bajiao.common.api.vo.Result;
import com.bajiao.entity.discountRule.DiscountRule;
import com.bajiao.service.discountRule.DiscountRuleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.bajiao.admin.base.AdminBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 店铺优惠规则
 * @Author: FanJq
 * @Date: 2025-01-13
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "店铺优惠规则")
@RestController
@RequestMapping("/discount/rule")
public class DiscountRuleController extends AdminBaseController<DiscountRule, DiscountRuleService> {
    @Autowired
    private DiscountRuleService discountRuleService;

    /**
     * @return
     * @description: 分页列表查询
     * @author: FanJq
     * @date: 2025-01-13
     */
    @ApiOperation(value = "店铺优惠规则-分页列表查询", notes = "店铺优惠规则-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(String storeId) {
        LambdaQueryWrapper<DiscountRule> sqlQuery = new LambdaQueryWrapper<>();
        sqlQuery.eq(StrUtil.isNotBlank(storeId), DiscountRule::getStoreId, storeId);
        sqlQuery.orderByAsc(DiscountRule::getStoreId);
        sqlQuery.orderByAsc(DiscountRule::getSeq);
        startPage();
        return Result.ok(getDataTable(discountRuleService.list(sqlQuery)));
    }

    /**
     * @param discountRule
     * @return
     * @description: 添加
     * @author: FanJq
     * @date: 2025-01-13
     */
    @ApiOperation(value = "店铺优惠规则-添加", notes = "店铺优惠规则-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody DiscountRule discountRule) {
        discountRuleService.save(discountRule);
        return Result.ok("添加成功！");
    }

    /**
     * @param discountRule
     * @return
     * @description: 编辑
     * @author: FanJq
     * @date: 2025-01-13
     */
    @ApiOperation(value = "店铺优惠规则-编辑", notes = "店铺优惠规则-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody DiscountRule discountRule) {
        discountRuleService.updateById(discountRule);
        return Result.ok("编辑成功!");
    }

    /**
     * @param id
     * @return
     * @description: 通过id删除
     * @author: FanJq
     * @date: 2025-01-13
     */
    @ApiOperation(value = "店铺优惠规则-通过id删除", notes = "店铺优惠规则-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        discountRuleService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * @param ids
     * @return
     * @description: 批量删除
     * @author: FanJq
     * @date: 2025-01-13
     */
    @ApiOperation(value = "店铺优惠规则-批量删除", notes = "店铺优惠规则-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.discountRuleService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * @param id
     * @return
     * @description: 通过id查询
     * @author: FanJq
     * @date: 2025-01-13
     */
    @ApiOperation(value = "店铺优惠规则-通过id查询", notes = "店铺优惠规则-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        DiscountRule discountRule = discountRuleService.getById(id);
        return Result.ok(discountRule);
    }

    /**
     * @param request
     * @param discountRule
     * @description: 导出excel
     * @author: FanJq
     * @date: 2025-01-13
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DiscountRule discountRule) {
        return super.exportXls(request, discountRule, DiscountRule.class, "店铺优惠规则");
    }

    /**
     * @param request
     * @param response
     * @return
     * @description: 通过excel导入数据
     * @author: FanJq
     * @date: 2025-01-13
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DiscountRule.class);
    }

}
